﻿using AutoMapper;
using MediatR;
using SmartProduction.Domain.客户管理;
using SmartProduction.ErrorCode;
using SmartProduction.Infarstructrue.Interface;
using SmartProduction.Write.API.Application.Commands.ResourceInfoCommand;

namespace SmartProduction.Write.API.Application.CommandHandlers.ResourceInfoHandler
{
    public class ResourceInfoCreateCommandHandler : IRequestHandler<ResourceInfoCreateCommand, APIResult<object>>
    {
        /// <summary>
        /// 资源信息仓储
        /// </summary>
        private readonly IBaseRepository<ResourceInfo> resourceRepository;

        /// <summary>
        /// 映射
        /// </summary>
        private readonly IMapper mapper;

        /// <summary>
        /// 构造方法
        /// </summary>
        /// <param name="resourceRepository">资源信息仓储</param>
        /// <param name="mapper">映射</param>
        public ResourceInfoCreateCommandHandler(IBaseRepository<ResourceInfo> resourceRepository,IMapper mapper)
        {
            this.resourceRepository = resourceRepository;
            this.mapper = mapper;
        }


        /// <summary>
        /// 处理
        /// </summary>
        /// <param name="request">请求</param>
        /// <param name="cancellationToken">取消</param>
        public async Task<APIResult<object>> Handle(ResourceInfoCreateCommand request, CancellationToken cancellationToken)
        {
            APIResult<object> result = new APIResult<object>();
       
            var obj = mapper.Map<ResourceInfo>(request);
            var  r =resourceRepository.AddAsync(obj).Result;

            if (r > 0)
            {
                result.Code = APIEnums.添加成功;
                result.Msg = APIEnums.添加成功.ToString();
                result.data = r;
            }
            else
            {
                result.Code = APIEnums.添加失败;
                result.Msg = APIEnums.添加失败.ToString();
                result.data = r;
            }
          
            return await Task.FromResult(result);     
        }
    }
}
